import { Column, CreateDateColumn, Entity, Generated, PrimaryGeneratedColumn, UpdateDateColumn, OneToMany, ManyToMany, JoinTable } from "typeorm";
import { UserHobby } from "./user_hobby.entity";
import { Article } from "src/article/entities/article.entity";
import { ArticleComment } from "src/article/entities/comment.entity";
import { Message } from "src/message/entities/message.entity";
@Entity({ name: 'user_info' })
export class UserAccount {
    @PrimaryGeneratedColumn()
    id: number

    //超级用户
    @Column({ default: false })
    root: boolean

    //封禁状态
    @Column({ default: false })
    banned: boolean

    @Column()
    @Generated('uuid')
    uuid: string

    //存放
    @Column({ nullable: true })
    avatar: string;


    @Column({ type: 'varchar', length: 255 })
    account: string

    @Column({ type: 'varchar', length: 255 })
    password: string

    //用户昵称
    @Column({ length: 255, type: String, comment: '用户昵称', nullable: true })
    nickname: string

    //用户性别  
    @Column({ type: 'enum', enum: ['男', '女', '保密'], comment: '用户性别', default: '保密' })
    gender: '男' | '女' | '保密'

    //用户生日
    @Column({ length: 255, type: String, comment: '用户生日', nullable: true })
    birthday: string
    //用户邮箱 
    @Column({ length: 255, type: String, comment: '用户邮箱', nullable: true })
    email: string
    //用户手机号
    @Column({ length: 11, type: 'char', comment: '用户手机号', nullable: true })
    phone: number
    //用户地址
    @Column({ length: 255, type: String, comment: '用户地址', nullable: true })
    address: string
    //用户个人简介
    @Column({ length: 255, type: String, comment: '用户个人简介', nullable: true })
    introduction: string
    //用户职业
    @Column({ length: 255, type: String, comment: '用户职业', nullable: true })
    profession: string

    @CreateDateColumn({ type: "timestamp", name: 'create_time' })
    create_time: Date;

    @UpdateDateColumn({
        name: 'update_time',
        nullable: true,
    })
    updateTime: Date | null;


    @OneToMany(type => UserHobby, hobbies => hobbies.userAccount, { onDelete: 'CASCADE', cascade: true })
    hobbies: UserHobby[]

    @OneToMany(type => Article, articles => articles.userAccount, { onDelete: 'CASCADE' })
    articles: Article[]

    @ManyToMany(type => Article, collections => collections.beCollected, { cascade: true, onDelete: 'CASCADE' })
    @JoinTable({
        name: 'user_collection_article',
        joinColumn: { name: 'user_id', referencedColumnName: 'id' },
        inverseJoinColumn: { name: 'article_id', referencedColumnName: 'id' },
    })
    collections: Article[]

    @ManyToMany(type => Article, likes => likes.liker, { cascade: true })
    @JoinTable({
        name: 'user_like_article',
        joinColumn: { name: 'user_id', referencedColumnName: 'id' },
        inverseJoinColumn: { name: 'article_id', referencedColumnName: 'id' },
    })
    likes: Article[]

    @ManyToMany(type => Article, views => views.viewer, { onDelete: 'CASCADE' })
    @JoinTable({
        name: 'user_viewer_article',
        joinColumn: { name: 'user_id', referencedColumnName: 'id' },
        inverseJoinColumn: { name: 'article_id', referencedColumnName: 'id' },
    })
    views: Article[]


    //粉丝
    @ManyToMany(type => UserAccount, followers => followers.followings, { onDelete: 'CASCADE' })
    follower: UserAccount[]

    //关注
    @ManyToMany(type => UserAccount, following => following.follower, { cascade: true, onDelete: 'CASCADE' })
    @JoinTable({
        name: 'user_follow_account',
        joinColumn: { name: 'user_id', referencedColumnName: 'id' },
        inverseJoinColumn: { name: 'user_id', referencedColumnName: 'id' },
    })
    followings: UserAccount[]

    @OneToMany(type => ArticleComment, comment => comment.userAccount, { cascade: true, onDelete: 'CASCADE' })
    comment: ArticleComment[]


    @ManyToMany(type => ArticleComment, commentLikes => commentLikes.likers, { onDelete: 'CASCADE' })
    commentLikes: ArticleComment[]


    //私信聊天
    @OneToMany(() => Message, message => message.sender, { onDelete: 'CASCADE' })
    messages: Message[];

    @OneToMany(() => Message, message => message.receiver, { onDelete: 'CASCADE' })
    received_messages: Message[];
}
